<?php
/**
 * SystemText data mapper
 *
 * Implements the Data Mapper design pattern
 *
 * @version $Id: SystemTextMapper.php 73 2010-04-25 00:30:44Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Default_Model_DbTable_SystemText
 * @package default
 * @subpackage Model
 */
class Default_Model_SystemTextMapper
{

    protected $_dbTable = null;

    protected $_groups = array("student","score","system");
    /**
     * Specify Zend_Db_Table instance to use for data operations
     *
     * @param Zend_Db_Table_Abstract $$dbTable
     * @return Default_Model_SystemTextMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
        	$dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
        	throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Default_Model_SystemText if no instance registered
     *
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
        $this->setDbTable('Default_Model_DbTable_SystemText');
        }
        return $this->_dbTable;
    }

    /**
     * Find a system_text entry by a key
     *
     * @param int $id
     * @param Default_Model_SystemText $system_text
     * @return void
     */
    public function save($system_text)
    {
        $data = array(
        	'sort' => $system_text->getSort(),
        	'kind' => $system_text->getKind(),
        	'group_id' => $system_text->getGroup_id(),
        	'id' => $system_text->getId(),
        	'name' => $system_text->getName(),
        	't_parent' => $system_text->getT_parent(),
        	'parent_sn' => $system_text->getParent_sn(),
        	'parent_dot' => $system_text->getParent_dot(),
        );
        if (null === ($id = $system_text->getSn()
        )) {
        	unset($data['id']);
        	$this->getDbTable()->insert($data);
        } else {
        	$this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    /**
     * Save a system_text entry
     *
     * @param Default_Model_SystemText $system_text
     * @return void
     */
    public function find($id, $system_text)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
        	return;
        }
        $row = $result->current();
        $system_text->setSn($row->sn)
        	->setSort($row->sort)
        	->setKind($row->kind)
        	->setGroup_id($row->group_id)
        	->setId($row->id)
        	->setName($row->name)
        	->setT_parent($row->t_parent)
        	->setParent_sn($row->parent_sn)
        	->setParent_dot($row->parent_dot)
        	;
    }

    /**
     * Fetch all system_text entries
     *
     * @return array
     */
    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
        	$entry = new Default_Model_SystemText();
        	$entry->setSn($row->sn)
        	->setSort($row->sort)
        	->setKind($row->kind)
        	->setGroup_id($row->group_id)
        	->setId($row->id)
        	->setName($row->name)
        	->setT_parent($row->t_parent)
        	->setParent_sn($row->parent_sn)
        	->setParent_dot($row->parent_dot)
        	;
        	$entries[] = $entry;
        }
        return $entries;
    }

    protected function _getTextByGroup($group_id)
    {
        $db = Zend_Db_Table::getDefaultAdapter();
        $query = $db->select()
        ->from('system_text',array('id'=>'sn','text'=>'name'))
        ->where('group_id=?',$group_id)
        ->where('parent_sn=0')
        ->order('sort');
        $res = $db->fetchAll($query);

        $arr = array();
        foreach($res as $row) {
            $query = $db->select()
            ->from("system_text",array('cc'=>'COUNT(*)'))
            ->where('parent_sn=?',$row['id']);
            $res2 = $db->fetchRow($query);
            //if ($res2['cc']>0)
            //    $row['hasChildren'] = true;
            $arr[] = $row;
        }
        return $arr;
    }

    public function getRoot()
    {
        $arr = array();
        foreach ($this->_groups as $val) {
            $arr2 = array();
            $arr2['text'] = $val;
            $arr2['children'] = $this->_getTextByGroup($val);
            $arr[] = $arr2;
        }
        return $arr;
    }

    public function getChildrens($sn)
    {
       return  $this->getDbTable()->fetchAll("parent_sn=$sn","sort")->toArray();
    }

    public function getKindArray($name)
    {
       // echo $name.'---';
         $res = $this->getDbTable()->fetchAll("kind='$name' AND parent_sn<>0")->toArray();
         $arr = array();
         foreach($res as $row)
             $arr[$row['id']] = $row['name'];
          return $arr;
    }
}
